package main

import (
	"code.google.com/p/goblinite/lib"
	"errors"
	"flag"
	"fmt"
	"os"
	"strconv"
	"time"
)

const command = "date"

func usage() {
	fmt.Fprint(os.Stderr, "usage: ", command, " [ -u | -n ] [ seconds ]\n")
	os.Exit(2)
}

var format = "Mon Jan 2 15:04:05 MST 2006"

var utc = flag.Bool("u", false, "report date as utc")
var unix = flag.Bool("n", false, "report date as unix time")

// Plan 9: prints "CES"; here: "CEST"
// Plan 9: ignore invalid/too big seconds; here: prints error message
// Plan 9: day printed right aligned; here: left aligned
// Plan 9: uses GMT, here: UTC
func main() {
	flag.Usage = usage
	flag.Parse()
	if flag.NArg() > 1 {
		usage()
	}
	now := time.Now()
	if flag.NArg() == 1 {
		if seconds, err := strconv.ParseInt(flag.Arg(0), 10, 64); err == nil {
			now = time.Unix(seconds, 0)
		} else {
			lib.Fatal(command, errors.New("bad argument"))
		}
	}
	switch {
	case *unix:
		fmt.Println(now.Unix())
	case *utc:
		fmt.Println(now.UTC().Format(format))
	default:
		fmt.Println(now.Format(format))
	}
	os.Exit(0)
}
